package com.ksd.pug.controller.article;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ksd.pug.commons.anno.PugLog;
import com.ksd.pug.commons.anno.PugPermissionIngore;
import com.ksd.pug.commons.enums.ResultStatusEnumA;
import com.ksd.pug.commons.ex.PugBussinessException;
import com.ksd.pug.commons.utils.fn.asserts.Vsserts;
import com.ksd.pug.config.anno.LoginCheck;
import com.ksd.pug.controller.BaseController;
import com.ksd.pug.pojo.Article;
import com.ksd.pug.service.article.IArticleService;
import com.ksd.pug.vo.comment.ArticleVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author: hcl
 * @date: 2022/1/24 15:03
 * @Description:
 */
@RestController
@Slf4j
public class ArticleController extends BaseController {

    @Resource
    private IArticleService articleService;

    /**
     * 查询文章列表信息并分页
     *
     * @return
     */
    @LoginCheck
    @PugPermissionIngore
    @PostMapping("/article/list")
    public IPage<Article> findArticlePage(@RequestBody ArticleVo articleVo) {
        return articleService.findArticlePage(articleVo);
    }
    @PostMapping("/article/list2")
    public IPage<Article> findArticlePage2(@RequestBody ArticleVo articleVo) {
        return articleService.findArticlePage(articleVo);
    }

    //查询自己的文章
    @PostMapping("/craetion/pagemy")
    public IPage<Article> findArticleMy(@RequestBody ArticleVo articleVo) {
        return articleService.findArticleMyPage(articleVo);
    }
    /**
     * 统计用户创作数
     *
     * @return
     */
    @PostMapping("/craetion/count/{userId}")
    public List<Map<String, Object>> countUserCraetion(@PathVariable("userId") Long userId) {
        return articleService.countUserCraetion(userId);
    }


    /**
     * 保存和修改文章
     *
     * @param article
     */

    @PostMapping("/article/saveupdate")
    @PugLog(title = "保存或修改文章")
    public Article saveUpdateArticle(@RequestBody Article article) {
        return articleService.saveUpdateArticle(article);
    }

    @PostMapping("/craetion/saveupdate")
    public Article saveUpdateArticle2(@RequestBody Article article) {
        return articleService.saveUpdateArticle(article);
    }

    /**
     * 根据文章id删除文章
     *
     * @param id
     * @return
     */
    @PostMapping("/article/del/{id}")
    @PugLog(title = "删除文章")
    public boolean delArticle(@PathVariable("id") Long id) {
        return articleService.delArticle(id);
    }

    /**
     * 根据文章ids批量删除文章
     *
     * @param articleVO
     * @return
     */
    @PostMapping("/article/delBatch")
    @PugLog(title = "批量删除文章")
    public boolean delArticle(@RequestBody ArticleVo articleVO) {
        log.info("你要批量删除的IDS是:{}", articleVO.getBatchIds());
        if (Vsserts.isEmpty(articleVO.getBatchIds())) {
            throw new PugBussinessException(ResultStatusEnumA.EMPTY_IDS_VALID);
        }
        return articleService.delBatchArticle(articleVO.getBatchIds());
    }


    /**
     * 根据文章id查询文章
     *
     * @param id
     * @return
     */
    @LoginCheck
    @PugPermissionIngore
    @GetMapping("/article/get/{id}")
    public Article getArticle(@PathVariable("id") Long id) {
        return articleService.getArticle(id);
    }

    /**
     * 查询热点文章
     *
     * @return
     */
    @LoginCheck
    @PugPermissionIngore
    @PostMapping("/article/getTop")
    public List<Article> findArticleTop(@RequestBody ArticleVo articleVo) {
        return articleService.findArticleTop(articleVo);
    }

}
